#include <iostream>
#include <algorithm>
using namespace std;
const int &MAX = 100000;
inline int parent(int i) {return i / 2;}
inline int left(int i) {return i * 2;}
inline int right(int i) {return 2 * i + 1;}
int h, i, A[MAX + 1];
void maxHeapify(int i) {
    int l = left(i), r = right(i), lgest;
    if (l <= h && A[l] > A[i]) lgest = l;
    else lgest = i;
    if (r <= h && A[r] > A[lgest]) lgest = r;
    if (lgest != i) {
        std::swap(A[i], A[lgest]);
        maxHeapify(lgest);
    }
}
int main() {
    

    cin >> h;
    for (i = 1; i <= h; ++i) cin >> A[i];

    for (int i = h / 2; i >= 1; --i) maxHeapify(i);
    for (int i = 1; i <= h; ++i) cout << " " << A[i];
    cout << endl;
    
    // for (i = 1; i <= h; ++i) {
    //     cout << "node " << i << " key = " << A[i] << ", ";
    //     if (parent(i) >= 1) cout << "parent key = " << A[parent(i)] << ", ";
    //     if (left(i) <= h) cout << "left key = " << A[left(i)] << ", ";
    //     if (right(i) <= h) cout << "right key = " << A[right(i)] << ", ";
    //     cout << endl;
    // }
    
    return 0;
}